import {
	HTTP_REQUEST_URL,
	STOREAGE
} from "@/config/app.js"

/**
 * 图片上传
 */
export const uploadImageOne = (opt, successCallback, errorCallback) => {
	let that = this;
	let token = uni.getStorageSync(STOREAGE.TOKEN)
	if (typeof opt === 'string') {
		let url = opt;
		opt = {};
		opt.url = url;
	}
	let count = opt.count || 1,
		sizeType = opt.sizeType || ['compressed'],
		sourceType = opt.sourceType || ['album', 'camera'],
		is_load = opt.is_load || true,
		uploadUrl = opt.url || '',
		inputName = opt.name || 'pics',
		fileType = opt.fileType || 'image';
	uni.chooseImage({
		count: count, //最多可以选择的图片总数  
		sizeType: sizeType, // 可以指定是原图还是压缩图，默认二者都有  
		sourceType: sourceType, // 可以指定来源是相册还是相机，默认二者都有  
		success: function(res) {
			//启动上传等待中...  
			uni.showLoading({
				title: '图片上传中',
			});
			uni.uploadFile({
				url: HTTP_REQUEST_URL + uploadUrl,
				filePath: res.tempFilePaths[0],
				fileType: fileType,
				name: inputName,
				formData: {
					'filename': inputName
				},
				header: {
					// #ifdef MP
					"Content-Type": "multipart/form-data;charset=UTF-8",
					// #endif
					"Authori-zation": 'Bearer ' + token,
					"Accept": "application/json;charset=UTF-8"
				},
				success: function(res) {
					uni.hideLoading();
					if (res.statusCode == 403) {
						that.Tips({
							title: res.data
						});
					} else {
						let data = res.data ? JSON.parse(res.data) : {};
						if (data.status == 200) {
							successCallback && successCallback(data)
						} else {
							errorCallback && errorCallback(data);
							that.Tips({
								title: data.msg
							});
						}
					}
				},
				fail: function(res) {
					uni.hideLoading();
					that.Tips({
						title: '上传图片失败'
					});
				}
			})
		}
	})
}